package com.loocup.utils;

import com.loocup.model.Column;
import com.loocup.model.Table;
import com.loocup.model.TableSheet;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 */
public class ExcelToTableUtils {

    public static List<Table> generateTable() {
        InputStream inputStream = null;
        try {
            List<Table> tables = new ArrayList<Table>();
            Properties properties = System.getProperties();
            String temeplateFilePath = StringUtils.join(new String[]{properties.getProperty("user.dir"), "src", "main", "resources", "db-template.xlsx"}, File.separator);
            inputStream = new FileInputStream(new File(temeplateFilePath));
            Workbook workbook = new XSSFWorkbook(inputStream);
            int numberOfSheets = workbook.getNumberOfSheets();
            // 每个sheet一个table
            for (int i = 0; i < numberOfSheets; i++) {
                Sheet sheet = workbook.getSheetAt(i);
                if ("目录".equals(sheet.getSheetName())){
                    continue;
                }
                Table table = new Table();
                Row tableInfoRow = sheet.getRow(0);
                table.setName(sheet.getSheetName());
                table.setDesc(tableInfoRow.getCell(0).getStringCellValue());
                table.setDbType(tableInfoRow.getCell(1).getStringCellValue());
                List<Column> columns = new ArrayList<Column>();
                for (int j = 2; j <= sheet.getLastRowNum(); j++) {
                    Row col = sheet.getRow(j);
                    Column column = new Column();
                    column.setDesc(getStringCellValue(col.getCell(0)));
                    column.setName(getStringCellValue(col.getCell(1)));
                    column.setType(getStringCellValue(col.getCell(2)));
                    column.setDefaultValue(getStringCellValue(col.getCell(3)));
                    column.setAllowNull(getStringCellValue(col.getCell(4)));
                    column.setIsPrimaryKey(getStringCellValue(col.getCell(5)));
                    column.setForeignKey(getStringCellValue(col.getCell(7)));
                    column.setIfAutoIncrement(getStringCellValue(col.getCell(8)));
                    column.setNote(getStringCellValue(col.getCell(9)));
                    columns.add(column);
                }
                table.setColumns(columns);
                tables.add(table);
            }
            return tables;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static String getStringCellValue(Cell cell){
        if (cell == null) {
            return "";
        }
        if (cell.getCellType().equals(CellType.STRING)){
            return cell.getStringCellValue();
        }else if (cell.getCellType().equals(CellType.NUMERIC)){
            return String.valueOf(cell.getNumericCellValue());
        }else if (cell.getCellType().equals(CellType.BLANK)){
            return "";
        } else {
            throw new RuntimeException(String.format("赞不支持的类型 %s, 位置%s-[%d,%d]", cell.getCellType(), cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex()));
        }
    }
}
